<?php
/**
 * 订单管理控制器
 * User: 吴
 * Date: 2019/3/14 0012
 * Time: 下午 13:32
 */

namespace app\admin\controller;

use think\Db;
use think\Request;
use cmf\controller\AdminBaseController;


class OrderController extends AdminBaseController{

    /**
     * 订单列表
     */
    public function index(){
        $where = '';
        if($this->request->isPost()){
           if(!empty($_POST['order_no'])){
              $where .= 'order_no = '.$_POST['order_no'];
           }
           if(!empty($_POST['order_status'])){
             if(empty($where)){
                $where .= 'order_status = '.$_POST['order_status'];
             }else{
                $where .= ' AND order_status = '.$_POST['order_status'];
             }
           }
        }
        $data = Db::name('order')->where($where)->order('order_id desc')->paginate(10);
        $status = ['待付款','待发货','待收货','已收货','已完成','退款中','已退款','待退货','待换货','退货中','换货中','拒绝退货','拒绝换货','已关闭'];
        $page = $data->render();
        $total = $data->total();
        $this->assign('page',$page);
        $this->assign('data',$data);
        $this->assign('total',$total);
        $this->assign('status',$status);
        return $this->fetch();
    }



    /**
     * 发货
     */
    public function shipments(){
        if($this->request->isGet()){
           $order_id = $this->request->get('order_id');
           $data = Db::name('order')->where('order_id',$order_id)->find();
           $this->assign('data',$data);
        }
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $express_company = $this->request->post('express_company');
            $express_no = $this->request->post('express_no');
            $order_info = Db::name('order')->where('order_id',$order_id)->find();
            $data_info = [
              'order_id'        => $order_id,
              'goods_id'        => $order_info['goods_id'],
              'express_company' => $express_company,
              'express_no'      => $express_no,
              'user_id'         => $order_info['user_id'],
              'user_name'       => $order_info['user_name'],
              'shipping_time'   => date('Y-m-d H:i:s',time()),
              'status'          => 0,
            ];
            $res = Db::name('order_express')->insert($data_info);
            if(!$res){
                $this->error('发货失败');
            }
            $order = Db::name('order')->where('order_id',$order_id)->update(['order_status' => 2,'shipping_time'=>date('Y-m-d H:i:s',time())]);
            if($order){
                $this->success('发货成功');
            }else{
                $this->error('发货失败');
            }
        }
        return $this->fetch('shipments');
    }



    /**
     * 查看物流
     */
    public function lookLogistics(){
        $order_id = $_GET['order_id'];
        $type = $_GET['type'];
        $data = Db::name('order')
              ->alias('a')
              ->field('a.order_no,b.*')
              ->join('order_express b','a.order_id=b.order_id','left')
              ->where('a.order_id',$order_id)
              ->where('b.type',$type)
              ->find();
        $status = ['送货中','揽件','寄送出了问题','已签收','退签','派件','退回'];
        if($data){
            $data['status'] = $status[$data['status']];
            //物流
            $res = $this->express($data['express_no']);
            $info = json_decode($res,true);
            if($info['message'] != 'ok'){
                $info = 0;
            }
        }else{
            $info = 0;
        }
        $this->assign('info',$info);
        $this->assign('data',$data);
        return $this->fetch('logistics');
    }



    /**
     *查询物流
     *
     * $no = 物流单号
     */
    public function express($no){
        $host = "https://kuaidi101.market.alicloudapi.com";
        $path = "/getExpress";
        $method = "GET";
        $appcode = "eaf8824a23384736b8cde69e89073829";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "NO=".$no."&TYPE=AUTO";
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        return curl_exec($curl);
    }



    /**
     * 退货
     */
    public function exchange(){
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $type = $this->request->post('type');
            if($type == 1){
                $order_status = 9;
            }else{
                $order_status = 11;
            }
            $res = Db::name('order')->where('order_id',$order_id)->update(['order_status'=>$order_status]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }



    /**
     * 退货收货
     */
    public function exchangeReceive(){
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $type = $this->request->post('type');
            if($type == 1){
                $order_status = 5;
            }else{
                $order_status = 1;
            }
            $res = Db::name('order')->where('order_id',$order_id)->update(['order_status'=>$order_status]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }



    /**
     * 换货
     */
    public function replace(){
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $type = $this->request->post('type');
            if($type == 1){
                $order_status = 10;
            }else{
                $order_status = 12;
            }
            $res = Db::name('order')->where('order_id',$order_id)->update(['order_status'=>$order_status]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }



}